package trastienda.web;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import trastienda.excepcion.DAOExcepcion;
import trastienda.modelo.Categoria;
import trastienda.service.CategoriaService;

@Controller
public class CategoriaController {

	@Autowired
	private CategoriaService categoriaService;
	
	public void setCategoriaService(CategoriaService categoriaService) {
		this.categoriaService = categoriaService;
	}

	@RequestMapping(value = "/categorias_listar", method = RequestMethod.GET)
	protected ModelAndView listar(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Collection<Categoria> lista = null;
		lista = categoriaService.listarCategorias();
		return new ModelAndView("categorias_listar", "CATEGORIAS", lista);
	}
	
	@RequestMapping(value = "/verRegistroCategoria", method = RequestMethod.GET)
	protected ModelAndView ingresar(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return new ModelAndView("categorias_nuevo");
	}
	
	@RequestMapping(value = "/registrarCategoria", method = RequestMethod.POST)
	protected ModelAndView registroCategoria(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("registarCategoria");
		
		Categoria cat = new Categoria();
		//cat.setIdCategoria(Integer.parseInt(request.getParameter("id")));
		cat.setNombre(request.getParameter("txtNombre"));
		cat.setDescripcion(request.getParameter("txtDescripcion"));
		
		try {
			categoriaService.insertarCategoria(cat);
		} catch (DAOExcepcion e) {
			return new ModelAndView("/error", "mensaje", e.getMessage());
		}

		return new ModelAndView("redirect:categorias_listar.html");
	}
	
	@RequestMapping(value = "/editarCategoria", method = RequestMethod.GET)
	protected ModelAndView editar(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Categoria cat = new Categoria();
		cat = categoriaService.obtenerPorIdCategoria(Integer.parseInt(request.getParameter("id")));
		
		System.out.println("idProducto "+request.getParameter("id"));
		return new ModelAndView("categorias_editar", "CATEGORIASEN", cat);
	}
	
	@RequestMapping(value = "/guardarModificacionCategoria", method = RequestMethod.POST)
	protected ModelAndView modificarCategoria(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("registarCategoria");
		
		Categoria cat = new Categoria();
		cat.setIdCategoria(Integer.parseInt(request.getParameter("id")));
		cat.setNombre(request.getParameter("txtNombre"));
		cat.setDescripcion(request.getParameter("txtDescripcion"));
		
		try {
			categoriaService.modificarCategoria(cat);
		} catch (DAOExcepcion e) {
			return new ModelAndView("/error", "mensaje", e.getMessage());
		}

		return new ModelAndView("redirect:categorias_listar.html");
	}
	
	@RequestMapping(value = "/eliminarCategoria", method = RequestMethod.GET)
	protected ModelAndView eliminarCategoria(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("eliminarCategoria");
		
		Categoria cat = new Categoria();
		cat.setIdCategoria(Integer.parseInt(request.getParameter("id")));
		cat.setNombre(request.getParameter("txtNombre"));
		cat.setDescripcion(request.getParameter("txtDescripcion"));
		
		try {
			categoriaService.eliminarCategoria(cat);
		} catch (DAOExcepcion e) {
			return new ModelAndView("/error", "mensaje", e.getMessage());
		}

		return new ModelAndView("redirect:categorias_listar.html");
	}
	
	
	
	
	
	
}
